package com.changfeng.service.impl;

import com.changfeng.bean.RetailersBanner;
import com.changfeng.bean.dto.vo.AdminBannerListVO;
import com.changfeng.bean.dto.vo.IndexBannerListVO;
import com.changfeng.dao.RetailersBannerMapper;
import com.changfeng.exception.ChangFengException;
import com.changfeng.exception.ChangFengExceptionEnum;
import com.changfeng.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BannerServiceImpl implements BannerService {

    @Autowired
    private RetailersBannerMapper retailersBannerMapper;
    @Override
    public List<AdminBannerListVO> findAll() {
        return retailersBannerMapper.findAll();
    }

    @Override
    public void saveBanner(AdminBannerListVO vo) {
        RetailersBanner retailersBanner = new RetailersBanner();
        retailersBanner.setBannerImgurl(vo.getBannerImgurl());
        retailersBanner.setBannerRepeaturl(vo.getBannerRepeaturl());
        retailersBanner.setBannerState(vo.getBannerState());
        retailersBanner.setGmtCreate(new Date());
        retailersBanner.setGmtModified(new Date());
        retailersBannerMapper.insert(retailersBanner);
    }

    @Override
    public void updateBanner(AdminBannerListVO vo) {
        RetailersBanner retailersBanner = retailersBannerMapper.selectByPrimaryKey(vo.getBannerId());
        if (retailersBanner == null) {
            throw new ChangFengException(ChangFengExceptionEnum.ERROR);
        }
        retailersBanner.setBannerImgurl(vo.getBannerImgurl());
        retailersBanner.setBannerRepeaturl(vo.getBannerRepeaturl());
        retailersBanner.setBannerState(vo.getBannerState());
        retailersBanner.setGmtModified(new Date());
        retailersBannerMapper.updateByPrimaryKey(retailersBanner);

    }

    @Override
    public void deleteBanner(Integer bannerId) {
        retailersBannerMapper.deleteByPrimaryKey(bannerId);
    }

    @Override
    public List<IndexBannerListVO> findIndexAll() {
        return retailersBannerMapper.findIndexAll();
    }

    @Override
    public AdminBannerListVO findById(Integer id) {
        RetailersBanner banner = retailersBannerMapper.selectByPrimaryKey(id);
        if (banner == null) {
            throw new ChangFengException(ChangFengExceptionEnum.ERROR);
        }
        AdminBannerListVO adminBannerListVO = new AdminBannerListVO();
        adminBannerListVO.setBannerId(banner.getBannerId());
        adminBannerListVO.setBannerImgurl(banner.getBannerImgurl());
        adminBannerListVO.setBannerRepeaturl(banner.getBannerRepeaturl());
        adminBannerListVO.setBannerState(banner.getBannerState());
        return adminBannerListVO;
    }
}
